package com.sdkj.action.admin;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.hibernate.HibernateException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sdkj.Entity.admin.AdminModule;
import com.sdkj.Entity.admin.AdminPrivs;
import com.sdkj.Entity.admin.AdminUser;
import com.sdkj.action.base.BaseAction;
import com.sdkj.service.admin.IAdminModuleService;
import com.sdkj.service.admin.IAdminUserService;
import com.sdkj.utils.CommonUtils;
import com.sdkj.utils.DateUtil;

@Controller
@Scope("prototype")
public class AdminUserAction extends BaseAction<AdminUser,IAdminUserService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private IAdminModuleService moduleService;
	
/*	public String login(){
		if(validAccAndPwd(model.getAccount(),model.getPwd())){
			return "INDEX";
		}
		return "LOGIN";
	}*/
	
	public void login(){
		JSONObject res=new JSONObject();
		if(validAccAndPwd(model.getAccount(), model.getPwd())){ 
			res.put(RES_MSG, "登录成功");
			res.put(RES_CODE, SUC_CODE);
			
			try {
				operationLogService.save(getOperationLogBean("login"));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else{
			res.put(RES_MSG, "用户名或密码错误");
			res.put(RES_CODE, -100); 
		}
		CommonUtils.writeJson(res); 
	}
	
	public void logout(){ 
		getSession().removeAttribute("curuser");
		getSession().invalidate();
		CommonUtils.writeJson("{}"); 
	}

	public void validOldPwd(){
		AdminUser sessionUser=(AdminUser) getSession().getAttribute("curuser");
		AdminUser user=service.login(sessionUser.getAccount(), model.getPwd());
		JSONObject res=new JSONObject();
		if(user!=null){
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "原密码正确");
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "原密码错误");
		}
		CommonUtils.writeJson(res);
	}
	
	public void updateUser(){
		AdminUser sessionUser=(AdminUser)getSession().getAttribute("curuser");
		JSONObject res=new JSONObject();
		if(validAccAndPwd(sessionUser.getAccount(),oldpwd)){
			try {
				if(service.update(sessionUser)){
					res.put(RES_CODE, 100);
					res.put(RES_MSG, "修改密码成功");
				}
			} catch (SQLException e) {
				res.put(RES_CODE, -100);
				res.put(RES_MSG, "修改密码失败");
				e.printStackTrace();
			}
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "原密码错误");
		}
		sessionUser.setPwd(model.getPwd());
		
		CommonUtils.writeJson(res);
	}
	
	public void regist() throws SQLException{
		String hql=" from AdminUser where account=:acc ";
		Map<String,Object>params=new HashMap<String,Object>();
		params.put("acc", model.getAccount());
		AdminUser user=null;
		JSONObject res=new JSONObject();
		try {
			user = service.findByHql(hql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(user!=null){
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "用户注册失败，账号已存在");
		}else{ 
			Serializable s=service.save(model);
			if(s!=null){
				res.put(RES_CODE, 100);
				res.put(RES_MSG, "注册用户成功"); 
			}else{
				res.put(RES_CODE, 100);
				res.put(RES_MSG, "注册用户失败");
			}
		}
		CommonUtils.writeJson(res);
	}
	/**
	 * 验证用户名是否被注册 
	 */
	public void validAcc(){
		String hql=" from AdminUser where account=:acc ";
		Map<String,Object>params=new HashMap<String,Object>();
		params.put("acc", model.getAccount());
		AdminUser user=null;
		JSONObject res=new JSONObject();
		try {
			user = service.findByHql(hql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(user==null){
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "账号未被注册");
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "账号已被注册"); 
		}
		CommonUtils.writeJson(res);
	}
	
	public void del() throws SQLException{
		boolean flag=service.deleteIds(ids);
		JSONObject res=new JSONObject();
		if(flag){
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "删除数据成功");
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "删除数据失败");
		}
		CommonUtils.writeJson(res);
	}
	
	public void getLoginInfo(){
		String sql="select DATE_FORMAT(operation_log.create_time,'%Y-%m') as remark,admin_user.account,CAST(count(1) AS CHAR) as portrait from operation_log,admin_user where admin_user.uuid=operation_log.uid and DATE_FORMAT(operation_log.create_time,'%Y-%m') > "+DateUtil.dt6_FromDate(DateUtil.getThreeMonthAgo(null))+" GROUP BY DATE_FORMAT(operation_log.create_time,'%Y-%m'),operation_log.uid";
		Map<String,Object>params=new HashMap<String,Object>();
//		params.put("dt", DateUtil.dt6_FromDate(DateUtil.getThreeMonthAgo(null)));
		try { 
			
			List<AdminUser> dts=service.findBySqlToBean(sql, params, page, 100);
			JSONObject res=new JSONObject();
			res.put("data", dts);
			CommonUtils.writeJson(res);
		} catch (HibernateException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	

	private boolean validAccAndPwd(String account, String pwd) {
		AdminUser user=service.login(account,pwd);
		if(user!=null){
			getSession().setAttribute("curuser", user);
			return true;
		}
		return false;
	} 
	
	public void getRoles() throws SQLException{
		HttpSession session=getSession();
		//  
		AdminUser user=(AdminUser) session.getAttribute("curuser");
//		if(user==null){
//			return;
//		}
		String uid=user.getUuid();
		List<AdminModule>list=moduleService.findByUid("402882a856532c670156533650ae0001");
		AdminUser u=service.findById(uid);
		List<AdminPrivs>privs=user.getPrivs();
		
//		StringBuilder bud=new StringBuilder("[");
/*		System.out.println(user.getRoles());
>>>>>>> lijian_dev
		AdminRoles r=null;
		List<AdminRoles>roles=user.getRoles();
		for(int i=0;i<roles.size();i++){
			r=roles.get(i);
			bud.append(r.toJsonStr());
			if(i<roles.size()-1){
				bud.append(",");
			} 
		}
		System.out.println(bud.toString());*/
//		List<AdminRoles> roles=user.getRoles();
//		
//		Set<AdminPrivs>privs=user.getPrivs();
//		
//		for(AdminRoles role:roles){
//			privs.addAll(role.getPrivs());
//		}
//			CommonUtils.writeJson(privs);
	}
	
	private String oldpwd;

	public String getOldpwd() {
		return oldpwd;
	}

	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}
	
	
	
}
